package com.example.lin.circleprogressbar.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.lin.circleprogressbar.R;

public class CircleProgressBar extends View {

    private static final int DEFAULT_WIDTH = 0;
    private static final int DEFAULT_HEIGHT = 0;
    private Paint paint;

    public CircleProgressBar(Context context) {
        super(context);
    }

    public CircleProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    private void init(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
        circleColor = typedArray.getColor(R.styleable.CircleProgressBar_circleColor, Color.GREEN);
        progressColor = typedArray.getColor(R.styleable.CircleProgressBar_progressColor, Color.BLUE);
        progressHint = typedArray.getColor(R.styleable.CircleProgressBar_progressHint, Color.GRAY);
        max = typedArray.getInteger(R.styleable.CircleProgressBar_max, DEFAULT_MAX);
        progress = typedArray.getInteger(R.styleable.CircleProgressBar_progress, DEFAULT_PROGRESS);
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int resultWidth = getSize(DEFAULT_WIDTH, widthMeasureSpec);
        int resultHeight = getSize(DEFAULT_HEIGHT, heightMeasureSpec);
        setMeasuredDimension(resultWidth, resultHeight);
        cy = getMeasuredHeight() / 2;
        cx = getMeasuredWidth() / 2;
        size = Math.min(getMeasuredWidth(), getMeasuredHeight());
    }

    private int size;
    private int cx;
    private int cy;

    private int getSize(int defaultSize, int measureSpec) {
        int result = defaultSize;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
            case MeasureSpec.AT_MOST:
                result = defaultSize;
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }

    private static final int DEFAULT_MAX = 100;
    private static final int DEFAULT_PROGRESS = 50;

    private int max;
    private int progress;
    private int progressHint;
    private int progressColor;
    private int circleColor;


    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
        if (getMeasuredWidth() == DEFAULT_WIDTH || getMeasuredHeight() == DEFAULT_HEIGHT)
            return;
        invalidate();
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        if (getMeasuredWidth() == DEFAULT_WIDTH || getMeasuredHeight() == DEFAULT_HEIGHT)
            return;
        invalidate();
    }


    private RectF rectF = new RectF();

    @Override
    protected void onDraw(Canvas canvas) {

        int maxR = size / 2;
        float p = 1.0f * progress / max;
        int strokeWidth = maxR / 16;
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(strokeWidth);
        paint.setColor(progressHint);
        int r2 = maxR - strokeWidth / 2;
        canvas.drawCircle(cx, cy, r2, paint);

        float sweepAngle = 360 * p;
        paint.setColor(progressColor);

        int k = strokeWidth / 2;
        rectF.set(cx - maxR + k, cy - maxR + k, cx + maxR - k, cy + maxR - k);
        canvas.drawArc(rectF, 90, -sweepAngle, false, paint);

        float r1 = maxR * p - strokeWidth;
        paint.setColor(circleColor);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(cx, cy, r1, paint);

    }
}
